package cn.jhc.bookstore.cn.jhc.bookstore.servlet;

import cn.jhc.bookstore.Constants;
import cn.jhc.bookstore.beans.Book;
import cn.jhc.bookstore.cn.jhc.bookstore.db.BookDao;
import com.google.gson.Gson;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.dbutils.QueryRunner;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/query_all_books.do")
public class QueryAllBooksServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        QueryRunner runner = (QueryRunner) req.getServletContext().getAttribute(Constants.QUERY_RUNNER);
        List<Book> books = BookDao.queryAll(runner);
        PrintWriter writer = resp.getWriter();
        writer.println(new Gson().toJson(books));
    }
}
